﻿@using MvcFileUploader.Models
@model MvcFileUploader.Models.FileUploadViewModel
@{
    var formId = "fileupload" + Guid.NewGuid();
    var fileInputId = "file" + Guid.NewGuid();
}
<form id="@formId" action="@Model.UploadUrl" method="POST" enctype="multipart/form-data">
    <div class="row fileupload-buttonbar">
        <div class="col-lg-10">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="glyphicon glyphicon-plus"></i>
                <span>Добавить файлы</span>
                <input type="file" name="files[]" multiple>
            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="glyphicon glyphicon-upload"></i>
                <span>Загрузить на сервер</span>
            </button>
        </div>

        <!-- The global file processing state -->
        <span class="fileupload-process"></span>
        <!-- The global progress state -->
        <div class="col-lg-5 fileupload-progress fade">
            <!-- The global progress bar -->
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                <div class="progress-bar progress-bar-success" style="width: 0%;"></div>
            </div>
            <!-- The extended global progress state -->
            <div class="progress-extended">&nbsp;</div>
        </div>
    </div>
    <!-- The table listing the files available for upload/download -->
    <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>

    <!-- Additional values to be posted with form-->
    @foreach (var postItem in Model.PostValuesWithUpload)
    {
        @Html.Hidden(postItem.Key, postItem.Value)
    }
</form>


<!--template and scripts-->
@if (Model.RenderSharedScript)
{
    <!-- The template to display files available for upload -->
    <script id="template-upload" type="text/x-tmpl">
        {% for (var i=0, file; file=o.files[i]; i++) { %}
        <tr class="template-upload fade">
            <td>
                <span class="preview"></span>
            </td>
            <td>
                <p class="name">{%=file.name%}</p>
                <strong class="error text-danger"></strong>
            </td>
            <td>
                <p class="size">Processing...</p>
                <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
            </td>
            <td>
                {% if (!i && !o.options.autoUpload) { %}
                <button class="btn btn-primary start">
                    <i class="glyphicon glyphicon-upload"></i>
                    <span>Старт</span>
                </button>
                {% } %}
                {% if (!i) { %}
                <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Отмена</span>
                </button>
                {% } %}
            </td>
        </tr>
        {% } %}
    </script>
    <!-- The template to display files available for download -->
    <script id="template-download" type="text/x-tmpl">
        {% for (var i=0, file; file=o.files[i]; i++) { %}
        <tr class="template-download fade">
            <td>
                <span class="preview">
                    {% if (file.thumbnailUrl) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery>
                        <img width="80" src="{%=file.thumbnailUrl%}">
                    </a>
                    {% } %}
                </span>
            </td>
            <td>
                <p class="name">
                    {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                    {% } else { %}
                    <span>{%=file.name%}</span>
                    {% } %}
                </p>
                {% if (file.error) { %}
                <div><span class="label label-danger">Error</span> {%=file.error%}</div>
                {% } %}
            </td>
            <td>
                <span class="size">{%=o.formatFileSize(file.size)%}</span>
            </td>
        </tr>
        {% } %}
    </script>

    <!-- Blueimp Jquery File Upload-->
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/tmpl.min.js"></script>
    <!-- The Load Image plugin is included for the preview images and image resizing functionality -->
    <script src="/Scripts/mvcfileupload/blueimp/load-image.min.js"></script>
    <!-- The Canvas to Blob plugin is included for image resizing functionality -->
    <script src="/Scripts/mvcfileupload/blueimp/canvas-to-blob.min.js"></script>

    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.iframe-transport.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.fileupload.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.fileupload-process.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.fileupload-image.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.fileupload-ui.js"></script>
    <script type="text/javascript" src="/Scripts/mvcfileupload/blueimp/jquery.fileupload-validate.js"></script>

    <!-- Blueimp styles / see blueimp docs for styling requirements for jquery ui -->
    <link rel="stylesheet" href="~/Content/mvcfileupload/jquery.fileupload.css">
    <link rel="stylesheet" href="~/Content/mvcfileupload/jquery.fileupload-ui.css">
}
<!--template and scripts end-->

<script type="text/javascript">

    // Initialize the jQuery File Upload widget:
    $('#@(formId)').fileupload({url: '@Model.UploadUrl'});

    $('#@(formId)').fileupload('option', {
        maxFileSize: @Model.MaxFileSizeInBytes,
        resizeMaxWidth: 1920,
        resizeMaxHeight: 1200,
        acceptFileTypes: @Model.FileTypes
        });

</script>